/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client.view;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.weissenborn.colabtex.client.places.Workplace;

/**
 * The Class GroupListViewImpl.
 */
public class GroupListViewImpl extends Composite implements GroupListView {

	/**
	 * The Interface Binder.
	 */
	interface Binder extends UiBinder<Widget, GroupListViewImpl> {
	}
	
	/** The Constant binder. */
	private static final Binder binder = GWT.create(Binder.class);
	
	/** The group panel. */
	@UiField StackLayoutPanel groupPanel;

	/** The listener. */
	private Presenter listener;
    
    /** The groups. */
    private List<String> groups;
	
	
	/**
	 * Instantiates a new group list view impl.
	 */
	public GroupListViewImpl() {
		initWidget(binder.createAndBindUi(this));
		groups = new ArrayList<String>();
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupListView#setPresenter(com.weissenborn.colabtex.client.view.GroupListView.Presenter)
	 */
	public void setPresenter(Presenter listener) {
		this.listener = listener;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupListView#addGroup(java.lang.String)
	 */
	public void addGroup(String groupName) {
		groups.add(groupName);
		
		VerticalPanel padPanel = new VerticalPanel();
		padPanel.setSpacing(4);
		groupPanel.add(new SimplePanel(padPanel), getHeader(groupName),25);	
	}

	/**
	 * Gets the header.
	 *
	 * @param groupName the group name
	 * @return the header
	 */
	private Widget getHeader(String groupName) {
		HTML html = new HTML(groupName);
		
		SimplePanel p = new SimplePanel(html);
		p.setStyleName(".gwt-StackPanel .gwt-StackPanelItem");
		return html;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupListView#addPad(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void addPad(String groupName, String padName, String padUrl) {		
		final Anchor padAnchor = new Anchor();
		padAnchor.setText(padName.substring(padName.indexOf("$")+1));
		padAnchor.setTarget(padUrl);

		((VerticalPanel)((SimplePanel)groupPanel.getWidget(groups.indexOf(groupName))).getWidget()).add( padAnchor);
		padAnchor.addClickHandler(new ClickHandler() {			
			public void onClick(ClickEvent event) {
				listener.goTo(new Workplace(padAnchor.getText(),padAnchor.getTarget()));
			}
		});
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupListView#removeGroup(java.lang.String)
	 */
	public void removeGroup(String groupName) {
		groupPanel.remove(groups.indexOf(groupName));
		
		groups.remove(groupName);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupListView#removePad(java.lang.String, java.lang.String)
	 */
	public void removePad(String groupName, String padName) {
		VerticalPanel panel = (VerticalPanel)((SimplePanel)groupPanel.getWidget(groups.indexOf(groupName))).getWidget();
		boolean found = false;
		int i = -1;
		while(!found&&i+1<panel.getWidgetCount()) {
			i++;
			Anchor pad = (Anchor)panel.getWidget(i);
			found = (pad.getHTML().equals(padName));
		}
		
		if(found)
			panel.remove(i);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupListView#getCurrentGroup()
	 */
	public String getCurrentGroup() {
		return groups.get(groupPanel.getVisibleIndex());
	}
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupListView#clear()
	 */
	public void clear() {
		groups.clear();
		groupPanel.clear();
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupListView#clearPads(java.lang.String)
	 */
	public void clearPads(String group) {
		((VerticalPanel)((SimplePanel)groupPanel.getWidget(groups.indexOf(group))).getWidget()).clear();
	}
}
